<?php


namespace app\index\model;


use app\BaseModel;

class AttachmentModel extends BaseModel
{
    protected  $name = 'attachment';

    //查询器
    public function searchIdsAttr($query, $ids, $data)
    {
        $query->where('id','in' ,$ids);
    }
    //创建
    public function createAttach($data)
    {
        $data['module_upload_dir'] = 0;
        $data['displayorder'] = 0;
        $data['group_id'] = !empty($data['group_id'])?$data['group_id']:0;
        $result =  $this->save($data);
        $result = !empty($result);
        return $result;
    }

    //更新
    public function updateAttach($filter ,$data)
    {
        $result =  $this->where($filter)->update($data);
        $result = !empty($result);
        return $result;
    }

    //获取列表
    public function listAttach($filter ,$page_config)
    {
        $start_row = ($page_config['page'] - 1) * $page_config['page_count'];
        $end_row   = $page_config['page_count'];
        $result    = $this->where($filter)
            ->order('createtime' ,'desc')
            ->limit($start_row ,$end_row)
            ->select();
        if(!empty($result)) $result = $result->toArray();
        return $result;
    }

    //获取数据总数
    public function listAttachCount($filter)
    {
        $result = $this->where($filter)
            ->count();
        return $result;
    }


    //获取
    public function getAttach($filter)
    {
        $result = $this->where($filter)->find();
        if(!empty($result)) $result = $result->toArray();
        return $result;
    }

    //删除
    public function delAttach($filter)
    {
        return $this->destoryAttach($filter);
    }

    //删除(真删除)
    public function destoryAttach($filter)
    {
//        $result = $this->withSearch(['ids'] ,$filter)->delete();
        $result = $this->where($filter)->delete();
        return !empty($result);
    }

    //批量移动文件分组
    public function moveAttach($filter,$data)
    {
        $result =  $this->where($filter)->update($data);
        return !empty($result);
    }

}